package com.jf.apply_car.controller;

import com.jf.apply_car.bean.Apply;
import com.jf.apply_car.service.ApplyService;
import com.jf.apply_car.util.LoginUtil;
import com.jf.apply_car.util.ResultJson;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/apply")
public class ApplyController {
    private final ApplyService applyService;

    public ApplyController(ApplyService applyService) {
        this.applyService = applyService;
    }

    @GetMapping("/apply")
    public String apply() {
        return "apply/apply";
    }

    @PostMapping("/apply")
    @ResponseBody
    public ResultJson apply(Apply apply) {
        applyService.apply(apply);
        return ResultJson.success();
    }

    @GetMapping("/my/list")
    public String myList(Model model) {
        model.addAttribute("pageUrl", "apply/my/page");
        return "apply/apply-list-all";
    }

    @GetMapping("/my/page")
    @ResponseBody
    public ResultJson myPage(int page, int limit, Apply apply) {
        return applyService.getMineApplyPage(page, limit, apply);
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultJson delete(int[] ids) {
        applyService.delete(ids);
        return ResultJson.success();
    }

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("pageUrl", "apply/page");
        return "apply/apply-list-all";
    }

    @GetMapping("/page")
    @ResponseBody
    public ResultJson page(int page, int limit, Apply apply) {
        return applyService.getAllPage(page, limit, apply);
    }

    @GetMapping("/exam/list")
    public String examList(Model model) {
        model.addAttribute("pageUrl", "apply/exam/page");
        return "apply/apply-list-all";
    }

    @GetMapping("/exam/page")
    @ResponseBody
    public ResultJson examPage(int page, int limit, Apply apply) {
        return applyService.getExamPage(page, limit, apply);
    }

    @GetMapping("/exam")
    public String exam() {
        if (LoginUtil.getLoginRoleId() == 1) {
            return "apply/apply-exam-admin";
        }
        return "apply/apply-exam-department";
    }

    @PostMapping("/exam")
    @ResponseBody
    public ResultJson exam(Apply apply) {
        applyService.exam(apply);
        return ResultJson.success();
    }

    @GetMapping("/info/{applyId}")
    @ResponseBody
    public ResultJson info(@PathVariable int applyId) {
        return applyService.getById(applyId);
    }

    @GetMapping("/return/list")
    public String returnList(Model model) {
        model.addAttribute("pageUrl", "apply/return/page");
        return "apply/apply-list-all";
    }

    @GetMapping("/return/page")
    @ResponseBody
    public ResultJson returnPage(int page, int limit, Apply apply) {
        return applyService.getReturnPage(page, limit, apply);
    }

    @PostMapping("/return/{applyId}")
    @ResponseBody
    public ResultJson returnCar(@PathVariable int applyId) {
        applyService.returnCar(applyId);
        return ResultJson.success();
    }

    @GetMapping("/detail")
    public String detail() {
        return "apply/apply-detail";
    }

}
